// 页面跳转
document.querySelector('.skip-register').addEventListener('click', function () {
  window.location.href = 'register.html'
})

function skipPage() {
  window.location.href = 'exam.html'
}
// 查看密码
document.querySelector('.passwd i').addEventListener('click', function () {
  let inputType = document.querySelector('.passwd input')
  inputType.type === 'password' ? (inputType.type = 'text', this.classList.toggle('icon-yanjing_yincang_o'), this.classList.add('icon-yanjing_xianshi_o')) : (inputType.type = 'password', this.classList.toggle('icon-yanjing_xianshi_o'), this.classList.add('icon-yanjing_yincang_o'))
})

// 关键字
let yzmK = 0
// 验证码
// 页面打开或刷新执行随机数函数
window.onload = function () {
  let num = yzm1()
  document.querySelector('.login-box-yzm .yzm').innerHTML = num
}
// 点击执行随机数函数
document.querySelector('.login-box-yzm .yzm').addEventListener('click', function () {
  let num = yzm1()
  this.innerHTML = num
})
// 随机数函数
function yzm1() {
  const yzmcs = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890'
  let yzm = ''
  for (let i = 0; i < 4; i++) {
    let random = Math.floor(Math.random() * yzmcs.length)
    yzm += yzmcs[random]
  }
  return yzm
}

// 验证码验证
// 验证码验证函数
function yzmYz() {
  let divYzm = document.querySelector('.login-box-yzm .yzm')
  let num = divYzm.innerText.toLowerCase()
  let yzm = document.querySelector('.login-box-yzm input')
  if (yzm.value === num || yzm.value === divYzm.innerText) {
    removeWarn(yzm)
    yzmK = 1
  } else if (!(yzm.value === num || yzm.value === divYzm.innerText)) {
    addWarn(yzm, '验证码错误')
    yzmK = 0
  }
}
// 失去焦点
document.querySelector('.login-box-yzm input').addEventListener('blur', function () {
  yzmYz()
})
//输入
document.querySelector('.login-box-yzm input').addEventListener('input', function () {
  yzmYz()
})

// 添加警告函数
function addWarn(inp, error) {
  const lab = inp.nextElementSibling
  const p = lab.nextElementSibling
  p.textContent = error
  p.classList.add('red')
  inp.classList.add('color')
}
// 移除警告函数
function removeWarn(inp) {
  const lab = inp.nextElementSibling
  const p = lab.nextElementSibling
  p.classList.remove('red')
  inp.classList.remove('color')
  p.innerHTML = ''
}

document.querySelector('.login-box-register .click-login').addEventListener('click', function () {
  let user = document.querySelector('.user input')
  let passwd = document.querySelector('.passwd input')
  let yzm = document.querySelector('.login-box-yzm input')
  if (user.value === '' || passwd.value === '' || yzm.value === '') {
    alert('请把表单信息填完整')
  }
  if (yzmK === 1) {
    loginUser(user.value, passwd.value)
  } else {
    yzmYz()
  }

})

let userMessage = JSON.parse(localStorage.getItem('user'))
//登录函数
function loginUser(username, password) {
  let root = document.querySelector('.icon .root')
  let stu = document.querySelector('.icon .stu')
  // 管理员登录
  if (root.checked) {
    for (let message of userMessage) {
      if (message.username === username && message.password === password && message.userType === '管理员') {
        let loginPage = []
        let obj = {
          username,
          password,
          userType: '管理员'
        }
        loginPage.push(obj)
        localStorage.setItem('login', JSON.stringify(loginPage))
        alert('登录成功')
        skipPage()
        return
      }
      else if (message.username === username && message.password !== password) {
        alert('密码错误')
        return
      }
    }
    alert('管理员未注册')
  }
  // 学生登录
  if (stu.checked) {
    for (let message of userMessage) {
      if (message.username === username && message.password === password && message.userType === '学生') {
        let loginPage = []
        let obj = {
          username,
          password,
          userType: '学生'
        }
        loginPage.push(obj)
        localStorage.setItem('login', JSON.stringify(loginPage))
        alert('登录成功')
        skipPage()
        return
      }
      else if (message.username === username && message.password !== password) {
        alert('密码错误')
        return
      }
    }
    alert('学生未注册')
  }

}
// 主题颜色
function colorSx() {
  let colors = localStorage.getItem('color')
  document.documentElement.style.setProperty('--themeColor', colors)
}
colorSx()